import json

import requests
from loguru import logger

from base.base import BaseRequest
from config import ENV


# from base.base import BaseLogin
class URL:
    _MENBER_LIST = "adminapi/member/member"
    _ADD_MEMBER = "adminapi/member/member"
    _MENBER_MODIFY = "adminapi/member/member/modify"
    _MENBER_MASSAGE = "adminapi/member/member/"
    _MEMBER_LABEL_LIST = "adminapi/member/label"
    _MEMBER_LABEL_ADD = "adminapi/member/label"
    _MEMBER_label_modify = "adminapi/member/label"
    _MEMBER_LABEL_DELETE = "adminapi/member/label"
    _MEMBER_LEVEL_LIST = "adminapi/member/level"
    # MEMBER_LEVEL_ADD=""
    _MEMBER_CONFIG_GROWTH_RULE = "adminapi/member/dict/growth_rule"
    _MEMBER_ACCOUNT_BALANCE = "adminapi/member/account/balance"
    _MEMBER_ACCOUNT_COMMISESSION = "adminapi/member/account/commission"


class Member(BaseRequest, URL):
    def member_list(self, keyword=None, register_type=None, label=None, channel=None, level=None, **kwargs):
        # 会员列表 支持条件查询
        """
        :param keyword:手机、邮箱、姓名、昵称
        :param register_type:注册来源
        :param label:会员标签
        :param channel:
        :param level:会员登级
        :return:response.json()
        """
        data = {
            # "method": "get",
            # "url": "",
            # "headers": {
            #     "token": self.token
            # },
            # "params": {
            'page': '1',
            'limit': '10',
            'keyword': keyword,
            'register_type': register_type,
            'member_label': label,
            'register_channel': channel,
            'member_level': level,
            **kwargs
            # }
        }
        # r = requests.request(**data)
        return self._get(url=self._MENBER_LIST, params=data)

    def add_member(self,nickname, member_no, mobile, password,**kwargs):  # 添加会员
        """
        :param member_id: 会员id
        :param name: 名字
        :param member_no:会员号
        :param init_member_no:
        :param mobile: 电话号
        :param password: 密码
        :return: response.json()
        """
        data = {
            # "method": "POST",
            # "url": "http://124.70.39.201/adminapi/member/member",
            # "headers": {
            #     "Content-Type": "application/json",
            #     "token": self.token
            # },
            # "json": {
            # "member_id": member_id,
            "nickname": nickname,
            "member_no": member_no,
            # "init_member_no": init_member_no,
            "mobile": mobile,
            "password": password,
            "password_copy": password
            # }
        }
        # response = requests.request(**data)
        self._post(url=self._ADD_MEMBER, data=data)
        return self._post(url=self._ADD_MEMBER, data=data)

    def member_modify(self, member_id, field, value):  # 会员编辑
        """
        :param member_id:
        :param field:性别 0:保密 1:男 2:女/ member_label:会员标签 / member_level:会员等级
        :param value:
        :return:
        """
        url = self._MENBER_MODIFY + f"/{member_id}/{field}"
        data = {
            # "method": "PUT",
            # "url": f"http://124.70.39.201/adminapi/member/member/modify/{member_id}/{field}",
            # "headers": {"Content-Type": "application/json", "token": self.token},
            # "json": {
            "member_id": member_id, "field": field, "value": value
            # }
            # {"member_id":200,"field":"sex","value":1}
        }

        # response = requests.request(**data)

        return self._put(url=url, data=data)

    def member_massage(self, member_id):  # 会员信息
        url = self._MENBER_MASSAGE + f"{member_id}"
        # data = {
        #     "method": "GET",
        #     "url": f"http://124.70.39.201/adminapi/member/member/{member_id}",
        #     "headers": {"token": self.token}
        # }
        # response = requests.request(**data)

        return self._get(url=url, params="")

    def member_labe_list(self, label_name):  # 标签列表
        data = {
            # 'method': 'GET',
            # 'url': 'http://124.70.39.201/adminapi/member/label',
            # 'headers': {'token': self.token},
            # 'params': {
            'page': '1', 'limit': '10', 'label_name': label_name
            # }
        }
        # response = requests.request(**data)
        return self._get(url=self._MEMBER_LABEL_LIST, data=data).json()

    def member_label_add(self,name,**kwargs):  # 标签添加
        data = {
            # 'method': 'get',
            # 'url': 'http://124.70.39.201/adminapi/member/member',
            # 'headers': {'token': self.token},
            # "params": {
            "label_id":"",
            "label_name":name,
            **kwargs
            # },
        }
        return self._get(url=self._MEMBER_LABEL_ADD, params=data)

    def member_label_modify(self, label_id, label_name, intro, sort):  # 标签编辑
        url = self._MEMBER_label_modify + f"/{label_id}"
        data = {
            # 'method': 'put',
            # 'url': f'http://124.70.39.201/adminapi/member/label/{label_id}',
            # 'headers': {'token': self.token, 'Content-Type': 'application/json'},
            # 'json': {
            'label_id': label_id, 'label_name': label_name, 'memo': intro, 'sort': sort
            # }
        }
        return self._put(url=url, data=data).json()

    def member_label_delete(self, label_id):  # 标签删除
        # data = {
        #     "method": "DELETE",
        #     "url": f"http://124.70.39.201/adminapi/member/label/{label_id}",
        #     "headers": {"token": self.token}
        #
        # }
        url = self._MEMBER_LABEL_DELETE + f"/{label_id}"

        return self._delete(url=url, params="")

    def member_level_list(self, level_nama):  # 会员等级列表
        data = {
            # "method": "GET",
            # "url": "http://124.70.39.201/adminapi/member/level",
            # "headers": {"token": self.token},
            # "params":{
            "page": 1,
            "limit": 10,
            "level_name": level_nama
            # }
        }
        return self._get(url=self._MEMBER_LEVEL_LIST, params=data)

    def member_level_add(self):  # 会员等级添加
        pass

    def member_config_growth_rule(self):  # 会员成长值规则配置查询
        data = {
            # "method": "Get",
            # "url": "http://124.70.39.201/adminapi/member/dict/growth_rule",
            # "headers": {"token": self.token},
            # "params": {
            "key": "member_register",
            "name": "会员注册",
            "desc": "会员注册之后发放成长值",
            "calculate": "",
            "content": {
                "admin": {}
            },
            "addon_name": "系统"
            # }
        }
        return self._get(url=self._MEMBER_CONFIG_GROWTH_RULE, params=data)

    def member_account_balance(self, keyword=None, type=None, time=None, mobile=None, id=None, b_type=None):  # 账户余额
        data = {
            # "url": "http://124.70.39.201/adminapi/member/account/balance",
            # "method": "get",
            # "header": {
            #     "token": ""
            # },
            # "params": {
            'page': '1',
            'limit': '10',
            'keywords': keyword,
            'from_type': type,
            'create_time': time,
            'mobile': mobile,
            'member_id': id,
            'balance_type': b_type,
            # }
        }
        return self._get(url=self._MEMBER_ACCOUNT_BALANCE, params=data)

    def member_account_commission(self, member_id, nickname, member_no, init_member_no, mobile, password):  # 账户佣金
        data = {
            # "method": "GET",
            # "url": "http://124.70.39.201/adminapi/member/account/commission",
            # "headers": {
            #     "Content-Type": "application/json; charset=utf-8",
            #     "token": self.token
            # },
            # "json": {
            "member_id": member_id,
            "nickname": nickname,
            "member_no": member_no,
            "init_member_no": init_member_no,
            "mobile": mobile,
            "password": password,
            "password_copy": password,
            # }
        }
        # logger.info(f"接口请求信息{data}")
        # response = requests.request(**data)
        # logger.info(f"接口返回信息{response.text}")
        return self._post(url=self._MEMBER_ACCOUNT_COMMISESSION, data=data)


if __name__ == '__main__':
    member = Member()
    print(json.dumps(member.member_list(), ensure_ascii=False, indent=2))
    # print(json.dumps(member.add_member(1101,"万雨鑫",1111,1212,15185843426,"wyx123456"), ensure_ascii=False, indent=2))
    # print(json.dumps(member.member_modify(200,"sex",1), ensure_ascii=False, indent=2))
    # print(member.member_massage(11))
